﻿using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Database.SQLite;

namespace AccelerateSensor.Service.DbProxy.SQLite.Dal
{
    internal class Node
    {
        private static readonly string TableName = SqliteHelper.TableNode;

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="model">节点对象</param>
        /// <returns>Rid</returns>
        public int Insert(Models.Node model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(Uuid, GatewayUuid, Type, LocalUpdateTime, UpdateTime) " +
                      "values(@Uuid, @GatewayUuid, @Type, @LocalUpdateTime, @UpdateTime);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加节点
        /// </summary>
        /// <param name="models">节点对象集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.Node> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(Uuid, GatewayUuid, Type, LocalUpdateTime, UpdateTime) " +
                      "values(@Uuid, @GatewayUuid, @Type, @LocalUpdateTime, @UpdateTime);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新节点(不修改节点Id)
        /// </summary>
        /// <param name="model">节点对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.Node model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set Uuid = @Uuid, GatewayUuid = @GatewayUuid, Type = @Type, " +
                      "    LocalUpdateTime = @LocalUpdateTime, UpdateTime = @UpdateTime " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 通过编号更新节点
        /// </summary>
        /// <param name="model">节点对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateByUuid(Models.Node model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set GatewayUuid = @GatewayUuid, Type = @Type, " +
                      "    LocalUpdateTime = @LocalUpdateTime, UpdateTime = @UpdateTime " +
                      "where Uuid = @Uuid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="model">节点对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.Node model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"delete from {TableName} where Rid = @Rid";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询节点
        /// </summary>
        /// <param name="rid">节点Id</param>
        /// <returns>节点对象</returns>
        public Models.Node GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.Node() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.Node>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有节点
        /// </summary>
        /// <returns>节点对象集合</returns>
        public List<Models.Node> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.Node>(sql);
        }
    }
}
